﻿using Microsoft.Extensions.Configuration;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace API.SqlSugar
{
    public class SqlSugarDapper
    {

        public SqlSugarClient DB
        {
            get
            {
                var ConnectionString = Config._config.str;
                
                return new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = Config._config.str,
                    DbType = DbType.SqlServer,//设置数据库类型
                    IsAutoCloseConnection = true,//自动释放数据务，如果存在事务，在事务结束后释放
                    InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息
                });
            }
        }

        public static SqlSugarDapper GetDbContext()
        {
            return new SqlSugarDapper();
        }

        public static SqlSugarClient GetDbTranContext()
        {
            return new SqlSugarDapper().DB;
        }
    }

    public class Config
    {
        private Config() { }

        private static readonly Config config = new Config();

        public static Config _config
        {
            get
            {
                return config;
            }
        }

        public string str { get; set; }

        public void GetSqlConnect(IConfiguration _Configuration)
        {
            str = _Configuration.GetConnectionString("DefaultConnection");
        }
    }
}
